* Creates Table 7 col 1
clear all

use "$savedata/masterdata.dta", replace

keep if sample25==1
gen vol = vol25

* Merge in trust characteristics
gen trust = trust_code
merge m:1 trust using "$inputs/types.dta"
drop if _merge==2
rename _merge _mergetrust
merge m:1 trust using "$staffdata/george_cardiologists_201718.dta"
drop if _merge==2
rename _merge _mergetrust2


* Create quality metric

preserve
reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe30, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe30 = docfe30*signal_2step

egen std_adj_docfe = std(adj_docfe)

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear, savefe) keepsingleton
predict residual2, residuals

xtreg residual2, fe i(hyid)
predict hospfe30, u

gen teach = 0
replace teach = 1 if trusttype==6
drop teaching

keep if finyear==2017 

collapse (mean) survive30 survive365 death_30 hospfe30 *docfe* has_cea count fte_contracted fte_actual british british_trained age female teach trusttype tenure_orig part_time_contracted (sum) one, by(trust_code)


foreach var in has_cea count fte_contracted fte_actual british british_trained age female tenure_orig part_time_contracted one{
	egen std_`var' = std(`var')
}


reg std_adj_docfe std_has_cea std_count std_one std_tenure_orig std_female std_age i.teach 
foreach var in has_cea count one tenure_orig female age{
			di _b[std_`var']
			matrix observe_`var' = _b[std_`var']
			
			}

foreach var in teach{
			di _b[1.`var']
			matrix observe_1`var' = _b[1.`var']
			}


restore

capture program drop myboot2

program define myboot2, rclass

* draw my bootstrap sample
preserve
bsample, strata(doctor_id)

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe30, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe30 = docfe30*signal_2step

egen std_adj_docfe = std(adj_docfe)

gen teach = 0
replace teach = 1 if trusttype==6
drop teaching

keep if finyear==2017 

collapse (mean) *docfe* has_cea count fte_contracted fte_actual british british_trained age female teach trusttype tenure_orig part_time_contracted (sum) one, by(trust_code)


foreach var in has_cea count fte_contracted fte_actual british british_trained age female tenure_orig part_time_contracted one{
	egen std_`var' = std(`var')
}

reg std_adj_docfe std_has_cea std_count std_one std_tenure_orig std_female std_age i.teach 
foreach var in has_cea count one tenure_orig female age{
			di _b[std_`var']
			return scalar d_`var' = _b[std_`var']
			}

foreach var in teach{
			di _b[1.`var']
			return scalar d_1`var' = _b[1.`var']
			}
			
			


		restore

end


** Then run bootstrap here

#delimit ;
* Simulate 199 times to test;
simulate diff_has_cea = r(d_has_cea) diff_count = r(d_count) diff_one = r(d_one) diff_tenure_orig = r(d_tenure_orig) diff_female = r(d_female) diff_age = r(d_age) diff_1teach = r(d_1teach)
, reps(199) seed(32786105): myboot2;


#delimit ;
* Boostrap;
bstat, stat(observe_has_cea, observe_count, observe_one, observe_tenure_orig,
observe_female, observe_age, observe_1teach);

#delimit cr
* Output results
putexcel set "$results/table7_col1.xlsx", replace
putexcel A1 = "Variable"
putexcel B1 = "Coefficient"
putexcel C1 = "Std. error"

matrix b = e(b)'
putexcel A2 = matrix(b), rownames

matrix se = e(se)'
putexcel C2 = matrix(se)

** Repeat for 365-day for column 2 of Table 7

use "$savedata/masterdata.dta", replace

keep if sample25==1
gen vol = vol25

* Merge in trust characteristics
gen trust = trust_code
merge m:1 trust using "$inputs/types.dta"
drop if _merge==2
rename _merge _mergetrust
merge m:1 trust using "$staffdata/george_cardiologists_201718.dta"
drop if _merge==2
rename _merge _mergetrust2


* Create quality metric
preserve
reghdfe survive365 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe365, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe365 = docfe365*signal_2step

egen std_adj_docfe = std(adj_docfe)

gen teach = 0
replace teach = 1 if trusttype==6
drop teaching

keep if finyear==2017 

collapse (mean) *docfe* has_cea count fte_contracted fte_actual british british_trained age female teach trusttype tenure_orig part_time_contracted (sum) one, by(trust_code)


foreach var in has_cea count fte_contracted fte_actual british british_trained age female tenure_orig part_time_contracted one{
	egen std_`var' = std(`var')
}


reg std_adj_docfe std_has_cea std_count std_one std_tenure_orig std_female std_age i.teach 
foreach var in has_cea count one tenure_orig female age{
			di _b[std_`var']
			matrix observe_`var' = _b[std_`var']
			
			}

foreach var in teach{
			di _b[1.`var']
			matrix observe_1`var' = _b[1.`var']
			}


restore

capture program drop myboot2

program define myboot2, rclass

* draw my bootstrap sample
preserve
bsample, strata(doctor_id)

reghdfe survive365 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe365, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe365 = docfe365*signal_2step

egen std_adj_docfe = std(adj_docfe)

gen teach = 0
replace teach = 1 if trusttype==6
drop teaching

keep if finyear==2017 

collapse (mean) *docfe* has_cea count fte_contracted fte_actual british british_trained age female teach trusttype tenure_orig part_time_contracted (sum) one, by(trust_code)


foreach var in has_cea count fte_contracted fte_actual british british_trained age female tenure_orig part_time_contracted one{
	egen std_`var' = std(`var')
}

reg std_adj_docfe std_has_cea std_count std_one std_tenure_orig std_female std_age i.teach 
foreach var in has_cea count one tenure_orig female age{
			di _b[std_`var']
			return scalar d_`var' = _b[std_`var']
			}

foreach var in teach{
			di _b[1.`var']
			return scalar d_1`var' = _b[1.`var']
			}
			
			


		restore

end


** Then run bootstrap here

#delimit ;
* Simulate 199 times to test;
simulate diff_has_cea = r(d_has_cea) diff_count = r(d_count) diff_one = r(d_one) diff_tenure_orig = r(d_tenure_orig) diff_female = r(d_female) diff_age = r(d_age) diff_1teach = r(d_1teach)
, reps(199) seed(32786105): myboot2;


#delimit ;
* Boostrap;
bstat, stat(observe_has_cea, observe_count, observe_one, observe_tenure_orig,
observe_female, observe_age, observe_1teach);

#delimit cr
* Output results
putexcel set "$results/Table7_col2.xlsx", replace
putexcel A1 = "Variable"
putexcel B1 = "Coefficient"
putexcel C1 = "Std. error"

matrix b = e(b)'
putexcel A2 = matrix(b), rownames

matrix se = e(se)'
putexcel C2 = matrix(se)



